package cn.android.weblog.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.android.weblog.R;
import cn.android.weblog.domain.Passage;

public class PassageListAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater layoutInflater;

    HashMap<Integer, View> location = new HashMap<>();
    private List<Passage> lists=new ArrayList<>();

    public PassageListAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }

    public void setData(List<Passage> lists){
        this.lists=lists;
        notifyDataSetChanged();
    }


    static class ViewHolder{

        public TextView title,time,describe;

        public ViewHolder(View itemView,Passage p){

            // 获取控件
            title=itemView.findViewById(R.id.passage_title);
            describe=itemView.findViewById(R.id.passage_describe);
            time=itemView.findViewById(R.id.passage_time);

            // 给控件赋值
            title.setText(p.getTitle());
            describe.setText(p.getDescribed());
            time.setText(p.getCreateDate().toString());

        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(location.get(position) == null) {
            convertView = layoutInflater.inflate(R.layout.item_passages_list,null);
            Passage passage=(Passage)getItem(position);
            holder = new ViewHolder(convertView,passage);
            location.put(position,convertView);
            convertView.setTag(holder);
        }else{
            convertView = location.get(position);
            holder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    /**
     * TODO:数据长度
     * @return
     */
    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


}

